﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
    public partial class DicItem : IDicItem
    {
        public enum PreContractStatus
        {
            Pending = 10,
            PartWon = 11,
            Won = 12
        }

        string SQL_SELECT_DICITEM = @"SELECT     DicType.DicTypeValue, DicItem.DicItemID, DicItem.DicItemValue, DicItem.DicItemDesc
                                            FROM         DicItem INNER JOIN
                                                                  DicType ON DicItem.DicTypeID = DicType.DicTypeID
                                            WHERE     (DicItem.IsEnabled = 1 AND DicType.DicTypeID = {0})";

        public const string SQL_SELECT_PROBLEMLVASSIGNMENT = @"select * from ProblemLVAssignment p left join DicItem d on d.DicItemID = p.ProblemLV2ID where d.IsEnabled = 1 ";

        //Get by filter
        [Transaction(TransactionMode.Requires)]
        public DataTable GetDicItemByDicType(BEGDC.PreContract.BLL.DicType.DictionaryType dictype)
        {
            using (ISession session = factory.OpenSession())
            {
                string strSql = string.Format(SQL_SELECT_DICITEM, Convert.ToString((int)dictype));

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
        }

        /// <summary>
        /// Query problemLV2 dicItem by problemLV1 which is in ProblemLVAssignment table
        /// </summary>
        /// <param name="problemLV1ID">Prolem　LV1 ID</param>
        /// <returns>DicItem List</returns>
        [Transaction(TransactionMode.Requires)]
        public DataTable GetProblemLV2ByLV1ID(int problemLV1ID)
        {
            using (ISession session = factory.OpenSession())
            {
                string strSql = SQL_SELECT_PROBLEMLVASSIGNMENT + string.Format(" and ProblemLV1ID = {0} ", problemLV1ID);
                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);
                return dtSearch;
            }
        }

        //get dic item info with it's type description
        [Transaction(TransactionMode.Requires)]
        public DataTable GetDicItemAndDicType_DataTable(string filter)
        {
            using (ISession session = factory.OpenSession())
            {
                string strSql = @"select dicitem.*, dictype.dictypevalue, dictype.dictypedesc from dbo.dicitem inner join dbo.dictype on dicitem.dictypeid=dictype.dictypeid";
                if (filter.Trim() != string.Empty)
                    strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);
                return dtSearch;
            }
        }

    }
}



